package com.hmkj.admin.controller.setting;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.core.constant.CacheID;
import com.hmkj.core.constant.State;
import com.hmkj.core.po.system.Configure;
import com.hmkj.core.service.cache.RedisService;
import com.hmkj.core.service.system.ConfigureService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 后台参数配置
 *
 * @author liweidong
 * @date 2017年03月10日 15:28
 */
@Controller
@RequestMapping(BaseController.BASE_URI)
public class ConfigureJsonController extends GenericController<Integer, Configure> {

    @Resource
    ConfigureService configureService;
    @Resource
    RedisService redisService;

    /**
     * 参数配置列表json数据
     *
     * @param dtGridPager
     * @return
     */
    @ResponseBody
    @RequestMapping("/config/json/list")
    @RequiresPermissions("sys:config:list")
    public DtGrid listJson(String dtGridPager) throws Exception {
        return configureService.getDtGridList(dtGridPager);
    }

    /**
     * 添加参数配置
     *
     * @param configure
     * @return
     */
    @ResponseBody
    @RequestMapping("/config/json/add")
    @RequiresPermissions("sys:config:save")
    public ResultEntity addJson(Configure configure) throws Exception {
        ResultEntity resultEntity = new ResultEntity();
        configureService.insert(configure);
        if (State.YES == configure.getStatus()) {
            redisService.put(CacheID.CONFIG_PREFIX + configure.getNid(), configure.getValue());
        }
        resultEntity.setCode(ResultEntity.SUCCESS);
        return resultEntity;
    }

    /**
     * 编辑参数配置
     *
     * @param configure
     * @return
     */
    @ResponseBody
    @RequestMapping("/config/json/edit")
    @RequiresPermissions("sys:config:edit")
    public ResultEntity editJson(Configure configure) throws Exception {
        ResultEntity resultEntity = new ResultEntity();
        configureService.updateByPrimaryKeySelective(configure);
        if (State.YES == configure.getStatus()) {
            redisService.put(CacheID.CONFIG_PREFIX + configure.getNid(), configure.getValue());
        } else {
            redisService.del(CacheID.CONFIG_PREFIX + configure.getNid());
        }
        resultEntity.setCode(ResultEntity.SUCCESS);
        return resultEntity;
    }

    /**
     * 删除参数配置
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/config/json/del")
    @RequiresPermissions("sys:config:del")
    public ResultEntity delJson(int id) throws Exception {
        ResultEntity resultEntity = new ResultEntity();
        configureService.removeById(new Configure(c -> c.setId(id)));
        Configure config = configureService.selectOne(new Configure(c -> {
            c.setId(id);
            c.setDelFlag(Configure.DELFLAG.NORMAL.code);
        }));
        if (config != null) {
            redisService.del(CacheID.CONFIG_PREFIX + config.getNid());
        }
        resultEntity.setCode(ResultEntity.SUCCESS);
        return resultEntity;
    }

}